//
//  AddLoveSignViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/16.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "AddLoveSignViewController.h"
#import "AddAddressViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "ZYButton.h"
#import "MyTagView.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface AddLoveSignViewController ()<TagViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataAray;
@property (nonatomic,strong) MyTagView *tagView;
@property (nonatomic,strong) NSMutableArray *selectedArray;
@end

@implementation AddLoveSignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"兴趣标签"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 40, 40);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonNotice setTitle:@"确定" forState:UIControlStateNormal];
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(finishSelectedTags) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;
    
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    backgroundView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    [self.view addSubview:backgroundView];
    _dataAray = [[NSMutableArray alloc] init];
    
    [self request];

}
- (void)request
{
    NSString *urlStr = [[NSString stringWithFormat:@"%@/userCenter/sysTagListByUserID",APIYIGOU]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        _dataAray = [NSMutableArray arrayWithArray:allDic[@"data"]];
        [self initTagsView];
        NSLog(@"77777%@",allDic);
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}
- (void)initTagsView {
    if (!_tagView) {
        //        _tagView = [[TagView alloc]initWithTitle:@"标签" tagArray:_dataAray selectedTagArray:self.lastSelected frame:CGRectMake(0, 64,kScreenW,kScreenW) delegate:self];
        _tagView = [[MyTagView alloc]initWithTitle:@"标签" contentTitleArray:_dataAray  frame:CGRectMake(0, 0, kScreenW,kScreenW ) delegate:self];
        
    }
    [self.view addSubview:_tagView];
    
}
- (void)didSelectedTagArray:(NSMutableArray *)nameArray {
    self.selectedArray = [NSMutableArray arrayWithArray:nameArray];
//    TagSign *tag = self.selectedArray[0];
//    DYLog(@"id=%@,name=%@",tag.tagId,tag.name);
}

//添加
- (void)finishSelectedTags
{
    if (self.DidSelectedTags) {
        self.DidSelectedTags(self.selectedArray);
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/saveMyTagsByUserId",APIYIGOU];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[UserData usertoken] forHTTPHeaderField:@"Auth"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //NSDictionary *parameters = @{@"label_names" : @[@"123"]};
    //NSString *jsonStr = [self DataTOjsonString:@[@"123"]];
    //NSLog(@"jsonStr:%@",jsonStr);
    NSMutableArray *parametersArr = [NSMutableArray array];
    for (int i = 0; i<self.selectedArray.count; i++)
    {
        TagSign *tag = self.selectedArray[i];
        NSDictionary *dic = @{@"tag_id":tag.tagId,@"tag_name":tag.name};
        [parametersArr addObject:dic];
    }

    
    [manager POST:urlStr parameters:parametersArr success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSDictionary changeType:(NSDictionary *)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]];
        if ([[dict valueForKey:@"code"] intValue] == 100) {
            DYLog(@"123456");
            DYLog(@"result:%@", dict.description);
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error %ld",(long)error.code);
    }];
    
}

//返回
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
